/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* Copyright 2002-2003 by Mark Wolgemuth, All rights reserved.             */
/* Copyright 2002-2003 by Employease, Inc.  All rights reserved.           */
/*                                                                         */
/* Redistribution and use in source and binary forms, with or without      */
/* modification, are permitted provided that the following conditions      */
/* are met:                                                                */
/*                                                                         */
/*    1. Redistributions of source code must retain the above copyright    */
/* notice, this list of conditions and the following disclaimer.           */
/*    2. Redistributions in binary form must reproduce the above copyright */
/* notice, this list of conditions and the following disclaimer in the     */
/* documentation and/or other materials provided with the distribution.    */
/*                                                                         */
/* THIS SOFTWARE IS PROVIDED BY THE MOD_ATHENA PROJECT ``AS IS'' AND ANY   */
/* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE       */
/* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR      */
/* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FREEBSD PROJECT OR        */
/* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,   */
/* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,     */
/* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR      */
/* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY     */
/* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT            */
/* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE   */
/* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.    */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#ifndef ATH_H
#define ATH_H

#if !defined(OS2) && !defined(WIN32) && !defined(BEOS) && !defined(NETWARE)
#include "unixd.h"
#endif

#include "httpd.h"
#include "http_config.h"
#include "http_log.h"
#include "ap_config.h"

#include "ath_lb.h"
#include "auto_config.h"

/* the following can be changes using runtime directives */
#define      ATH_ENGINE_PATH     "/athena/" /* must have trailing / */
#define      ATH_SHM_PATH        "/tmp/.apache_mod_athena_shm"
#define      ATH_MUX_PREFIX      "/tmp/.apache_mod_athena_mux."
#define      ATH_ENGINE_ON       0          /* default to off */
#define      ATH_TTL             60         /* 60 seconds */

/* compile time only settings */
#define      ATH_MAX_EMBED       16

#ifdef ATH_INCLUDE_STATUS_DEBUG
#define        ATH_DEBUG_STATUS_FUNC TRUE
#else
#undef         ATH_DEBUG_STATUS_FUNC
#endif // ATH_INCLUDE_DEBUG_STATUS

/* default status formats */
#define      ATH_STAT_FARM_FMT   "%n: (%o) algo: %a %h expect update: %e ttl: %t rttl: %r (chosen: %c)\n   smartfarmable: %F down url: %d offline url: %D\n   members: %m\n"
#define      ATH_STAT_PHYS_FMT   "%t (%o/%u/%i) | %c | %n | %l | %m | %d | %0 | %1 | %2 | %3 | %4 | %5 | %6 | %7 | %8 | %9 <%h> [%f]"
#define      ATH_STAT_ENGI_FMT   "path: %P down url: %d farms: %f physicals: %p nominate per update (0/1): %n\ndefaults for farms:\n   algorithm: %a %h\n   expect update: %e\n   ttl: %t"

extern module AP_MODULE_DECLARE_DATA athena_module;

#define ATH_LOG_LABEL(s) "mod_athena[" s "]"
#define ATH_APLOG(lvl, rv, fmt, args...) \
  (ap_log_error(APLOG_MARK, lvl, rv, NULL, fmt, ##args))

#ifdef ATH_DEBUG
#define        DEBUGLOG(fmt, args...)                     \
  ATH_APLOG(APLOG_INFO, 0, "%s(%d,%s): " fmt,		  \
	    __FILE__, __LINE__, __FUNCTION__, ##args)
#else
#define        DEBUGLOG(fmt, args...) ((void)0)
#endif // ATH_DEBUG

/* apache api prototypes in mod_athena.c
 * they are static 
 */


/* config.c */
#define        ATH_CONF_AP_GET(server) \
   (ath_conf_ap *) ap_get_module_config(server->module_config, &athena_module)
ath_conf_ap  * ath_conf_ap_alloc(apr_pool_t *p);
int            ath_conf_is_on(const char *str_test);
/* *n = *dummy in apache diction, just saving space here */
const char * ath_conf_engine(cmd_parms *parms, void *n, int flag);
const char * ath_conf_default_farm_on(cmd_parms *parms, void *n, int flag);
const char * ath_conf_default_phys_on(cmd_parms *parms, void *n, int flag);
const char * ath_conf_mirror(cmd_parms *parms, void *n, int flag);
const char * ath_conf_diverse_multi(cmd_parms *parms, void *n, int flag);
const char * ath_conf_expectation(cmd_parms *parms, void *n, int flag);
const char * ath_conf_nomination(cmd_parms *parms, void *n, int flag);
const char * ath_conf_max_excluded(cmd_parms *parms, void *n, int flag);
const char * ath_conf_path(cmd_parms *parms, void *n, const char *arg);
const char * ath_conf_downurl(cmd_parms *parms, void *n, const char *arg);
const char * ath_conf_downurl_forceget(cmd_parms *parms, void *n, int flag);
const char * ath_conf_offlineurl(cmd_parms *parms, void *n, const char *arg);
const char * ath_conf_offlineurl_forceget(cmd_parms *parms, void *n, int flag);
const char * ath_conf_shmpath(cmd_parms *parms, void *n, const char *arg);
const char * ath_conf_muxpath(cmd_parms *parms, void *n, const char *arg);
const char * ath_conf_muxcount(cmd_parms *parms, void *n, const char *arg);
const char * ath_conf_expects_field(cmd_parms *parms, void *n, const char *arg);
const char * ath_conf_readonly(cmd_parms *parms, void *n, const char *arg);
const char * ath_conf_physmax(cmd_parms *parms, void *n, const char *arg);
const char * ath_conf_farmmax(cmd_parms *parms, void *n, const char *arg);
const char * ath_conf_physfmt(cmd_parms *parms, void *n, const char *arg);
const char * ath_conf_farmfmt(cmd_parms *parms, void *n, const char *arg);
const char * ath_conf_engifmt(cmd_parms *parms, void *n, const char *arg);
const char * ath_conf_ttl(cmd_parms *parms, void *n, const char *arg);
const char * ath_conf_recover_ttl(cmd_parms *parms, void *n, const char *arg);
const char * ath_conf_algorithm(cmd_parms *parms, void *n, const char *arg);
const char * ath_conf_hitadds(cmd_parms *parms, void *n, 
			      const char *arg1, const char *arg2);
const char * ath_conf_smartfarm_enable(cmd_parms *parms, void *dummy, int flag);
const char * ath_conf_smartfarm_protect(cmd_parms *parms, void *dummy, const char *arg);
const char * ath_conf_smartfarm_tamper_url(cmd_parms *parms, void *dummy, const char *arg);
const char * ath_conf_cookie_name_smartfarm(cmd_parms *parms, void *dummy, const char *arg);
const char * ath_conf_farm(cmd_parms *cmd, void *n, const char *arg);

/* request.c */
void           ath_request_update(request_rec *r);
void           ath_request_update_farm(apr_pool_t *p, char *str_upd);
void           ath_request_update_phys(apr_pool_t *p, char *str_upd);
void           ath_request_status(request_rec *r);
char *         ath_request_status_phys(apr_pool_t *p,
				      char *str_stat, ath_conf_ap *aconf);
char *         ath_request_status_farm(apr_pool_t *p,
				      char *str_stat, ath_conf_ap *aconf);
char *         ath_request_status_engine(apr_pool_t *p, char *str_stat);
char *         ath_request_status_debug(request_rec *r);
void           ath_request_mirror(request_rec *r);
void           ath_request_hello(request_rec *r);
void           ath_request_balance_proxyreq(request_rec *r);
void           ath_request_balance_updatereq(apr_pool_t *p, ath_phys *phys);
void           ath_request_balance_req(request_rec *r);

/* locks.c */
void           ath_lock_setpath(apr_pool_t *p, 
				ath_lock *lock, const char *prefix);
ath_lock *     ath_lock_create(apr_pool_t *p, ath_lock *lock);
void           ath_lock_init(apr_pool_t *p, ath_lock *lock);
ath_lock *     ath_lock_for_idx(int idx);
ath_lock *     ath_lock_raw_for_idx(int idx);
void           ath_lock_acquire(ath_lock *lock);
void           ath_lock_release(ath_lock *lock);
int            ath_lock_mux_init_count();

/* farm_safe.c */
void           ath_farm_update(apr_pool_t *pool, 
			       ath_farm *farm, ath_upd_farm *farm_upd);
ath_farm *     ath_farm_from_str(apr_pool_t *pool, char *str_farm);

/* physical_safe.c */
void           ath_phys_update(ath_phys *phys, ath_upd_phys *phys_upd);
ath_phys *     ath_phys_from_str(apr_pool_t *pool, char *str_phys);

/* algorithm_safe.c */
void           ath_algo_expect_update(ath_farm *farm, ath_phys *phys);
void           ath_algo_hit_adds(ath_farm *farm, ath_phys *phys);
int            ath_algo_expect_get_chosen(apr_pool_t *pool, ath_farm *farm, 
					  ath_algo_def *algo_def);


/* cookie.c */
char *         ath_cookie_getval(request_rec *r, const char *name);

/* smartfarm.c */
void           ath_smartfarm_init(apr_pool_t *p, const char *data);
void           ath_smartfarm_handler(request_rec *r, char **strfarm, 
				     const char *uripart1,
				     const char *uripart2);

#endif // ATH_H
